package com.main.util;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

/**
 * 邮件工具
 *
 * @author RuoYu
 * @date 2022/09/26
 */
@Component
public class MailUtils {

    @Autowired
    private JavaMailSender javaMailSender;

    @Value("${spring.mail.username}")
    private String from;//发送人

    //    private String to = "983985701@qq.com";//接收人

    @Value("${mail.subject}")
    private String subject;//邮件主题

    //邮件内容
    @Value("${mail.prefixTest}")
    private String prefixTest = "";

    @Value("${mail.suffixTest}")
    private String suffixTest = "";

    //    @Async
    public void sendMail(String to, String code) {
        try {
            SimpleMailMessage message = new SimpleMailMessage();
            message.setFrom(from + "(随易租)");
            message.setTo(to);
            message.setSubject(subject);
            message.setText(prefixTest + code + suffixTest);
//        message.setSentDate();//定时发送
            javaMailSender.send(message);
        } catch (MailException e) {
            e.printStackTrace();
        }
    }
}
